import UI_ChooseClient from "../../ui/Main/UI_ChooseClient";
import BaseUIMediator from "../../LTGame/UI/BaseUIMediator";
import { DebuggerManager } from "../debugger/DebuggerManager";
import UI_view_item_client from "../../ui/Main/UI_view_item_client";
import UI_HierarchyMediator from "./UI_HierarchyMediator";
import { EMessageS2C } from "../../common/EMessageS2C";
import { UIManager } from "./UIManager";
import UI_NoticeMediator, { NoticeData } from "./UI_NoticeMediator";

export default class UI_ChooseClientMediator extends BaseUIMediator<UI_ChooseClient> {

    private static _instance: UI_ChooseClientMediator;
    public static get instance(): UI_ChooseClientMediator {
        if (this._instance == null) {
            this._instance = new UI_ChooseClientMediator();
            this._instance._classDefine = UI_ChooseClient;
        }
        return this._instance;
    }

    private _isQuerying: boolean;

    private _cacheClientList: WrapClientData[];

    _OnShow() {
        super._OnShow();
        // your code
        this._SetToCenter();

        this._cacheClientList = [];

        this.ui.m_list_client.setVirtual();
        this.ui.m_list_client.itemRenderer = Laya.Handler.create(this, this._OnItemRender, null, false);
        this.ui.m_list_client.numItems = 0;

        this.ui.m_btn_refresh.onClick(this, this._OnClickRefresh);

        this._RefreshClientList();

        DebuggerManager.instance.RegistMsg(EMessageS2C.QueryClientList, this, this._OnClientUpdateMsg);
    }

    _OnHide() {
        DebuggerManager.instance.UnRegistMsg(EMessageS2C.QueryClientList);
    }

    private _OnItemRender(index: number, itemUI: UI_view_item_client) {
        let data = this._cacheClientList[index];
        if (data == null) return;
        itemUI.m_text_dress.text = data.address;
        itemUI.m_text_name.text = data.name;
        itemUI.m_btn_connect.onClick(this, this._OnClickConnect, [index]);
    }

    private async _OnClickConnect(index: number) {
        let clickClient = this._cacheClientList[index];
        if (clickClient == null) return;

        this.ui.m_view_load.visible = true;
        let result = await DebuggerManager.instance.ConnectClient(clickClient.id);
        this.ui.m_view_load.visible = false;

        if (result) {
            this.Hide();
            UIManager.OpenDebuggerPanels();
            DebuggerManager.instance.RegistMsg(EMessageS2C.ClientDisconnect, this, this._OnClientDisconnect, true);
        } else {
            console.error("连接失败");
        }

    }

    private _OnClientDisconnect() {
        let noticeData = new NoticeData();
        noticeData.context = '客户端已断开连接!';
        noticeData.onClose = Laya.Handler.create(this, () => {
            UIManager.HideDebuggerPanels();
            this.Show();
        });
        UI_NoticeMediator.instance.Show(noticeData);
    }

    private _RefreshClientList() {
        this._isQuerying = true;
        DebuggerManager.instance.QueryClientList();
        this.ui.m_view_load.visible = true;
    }

    private _OnClientUpdateMsg(msg: any) {
        this._cacheClientList = msg as WrapClientData[];
        this._isQuerying = false;
        this.ui.m_view_load.visible = false;
        this.ui.m_list_client.numItems = this._cacheClientList.length;
    }

    private _OnClickRefresh() {
        if (this._isQuerying) return;
        this._RefreshClientList();
    }

}

class WrapClientData {

    public id: number;
    public name: string;
    public address: string;

}